package com.cn.sx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.springframework.jdbc.UncategorizedSQLException;

public class action1 extends NonSessionAction{
	private static final Log log=LogFactory.getLog(action1.class);
	private com.cn.sx.bean1 bean1;

	public com.cn.sx.bean1 getBean1() {
		return bean1;
	}

	public void setBean1(com.cn.sx.bean1 bean1) {
		this.bean1 = bean1;
	}
	
    public ActionForward getfile(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
    	
    	
    	try{
    		
        String[] updatesql1=null;
        String[] updatesql=null;
        
    	String[] aa=bean1.getArrayType();
    	String tablename_1=aa[4];
    	String tablename_2=aa[5];
    	
    	String temp="";
    	String fileURL=request.getParameter("path"); 
    	String file_index=request.getParameter("file_index"); 
    	fileURL=URLDecoder.decode(fileURL,"UTF-8");
        form1 form1=(form1)form;
        FormFile file = form1.getTheFile(); //取得上传的文件
        FormFile file2 = form1.getTheFile1(); //取得上传的文件

        
        String filename=fileURL.substring(fileURL.lastIndexOf("\\")+1, fileURL.length());
        String index_flag_date=filename.substring(filename.lastIndexOf("_")+1, filename.lastIndexOf("."));
    	
        String reuqest1="";
        String reuqest23="";
        
        temp=file_index;
        log.info("temp:"+temp);
    	int count11=bean1.getcount1("custlist_"+temp);
    	log.info("count11:"+count11);
        if(count11>0){
    		request.setAttribute("result", "已有批次,不需要导入");
    		return mapping.findForward("fail");
        }
        
        if(file.getFileName().indexOf("INCUSTOM")!=-1){
            String tablename=bean1.gettableName();
        	String sql1=bean1.getsql1(bean1.getArrayType1(),bean1.getArrayType2(),tablename,bean1.getdatebaseType());
        	if(sql1.equals("fail")){
        		request.setAttribute("result", "前置SQL语句获取错误");
        		return mapping.findForward("fail");
        	}
        	
            InputStream stream = file.getInputStream(); //把文件读入
            InputStreamReader input = new InputStreamReader(stream);  
            
            updatesql=bean1.getupdatesql(input,sql1,bean1.getArrayType2(),tablename,file_index,aa[1]);
            log.info("updatesql:"+updatesql);
        	
        	int count=0;
        	for(int i=0;i<updatesql.length;i++){
        		int result_sql=0;
        		try{
        			if(updatesql[i]!=null&&updatesql[i].length()>0){
        				result_sql=bean1.update1(updatesql[i]);
        			}
        		}catch(Exception e){
        			result_sql=0;
                    bean1.update123("数据导入","SQL语法错误",updatesql[i].toString().replace("'",""));
            		log.info(bean1.getERRO(e));
        			reuqest1=reuqest1+e.toString()+"<br>";
        		}
        		count=count+result_sql;
        	}        	
            
    		String tablename1=bean1.gettableName1();   		
        	String sql2=bean1.getsql1(bean1.getArrayType3(),bean1.getArrayType4(),tablename1,bean1.getdatebaseType());
        	if(sql2.equals("fail")){
        		request.setAttribute("result", "前置SQL语句获取错误");
        		return mapping.findForward("fail");
        	}

            InputStream stream1 = file2.getInputStream(); //把文件读入
            InputStreamReader input1 = new InputStreamReader(stream1);
            
            updatesql1=bean1.getupdatesql(input1,sql2,bean1.getArrayType4(),tablename1,file_index,aa[1]);
       	
        	int count1=0;
        	for(int i=0;i<updatesql1.length;i++){
        		int result_sql=0;
        		try{
        			if(updatesql1[i]!=null&&updatesql1[i].length()>0){
        				result_sql=bean1.update1(updatesql1[i]);
        			}
        		}catch(Exception e){
            	    bean1.update123("数据导入","SQL语法错误",updatesql1[i].toString().replace("'",""));
        			result_sql=0;
        			log.info(bean1.getERRO(e));
        			reuqest1=reuqest1+e.toString()+"<br>";
        		}
        		count1=count1+result_sql;
        	}

        	
        	
        }else{
            String tablename=bean1.gettableName();
        	String sql1=bean1.getsql1(bean1.getArrayType1(),bean1.getArrayType2(),tablename,bean1.getdatebaseType());
        	if(sql1.equals("fail")){
        		request.setAttribute("result", "前置SQL语句获取错误");
        		return mapping.findForward("fail");
        	}

            InputStream stream = file2.getInputStream(); //把文件读入
            InputStreamReader input = new InputStreamReader(stream);      
            
            updatesql=bean1.getupdatesql(input,sql1,bean1.getArrayType2(),tablename,file_index,aa[1]);
            
        	int count=0;
        	for(int i=0;i<updatesql.length;i++){
        		int result_sql=0;
        		try{
        			if(updatesql[i]!=null&&updatesql[i].length()>0){
        				result_sql=bean1.update1(updatesql[i]);
        			}
        		}catch(Exception e){
            	    bean1.update123("数据导入","SQL语法错误",updatesql[i].toString().replace("'",""));
        			result_sql=0;
        			log.info(bean1.getERRO(e));
        			reuqest1=reuqest1+e.toString()+"<br>";
        		}
        		count=count+result_sql;
        	}
            
    		String tablename1=bean1.gettableName1();
    		
        	String sql2=bean1.getsql1(bean1.getArrayType3(),bean1.getArrayType4(),tablename1,bean1.getdatebaseType());
        	if(sql2.equals("fail")){
        		request.setAttribute("result", "前置SQL语句获取错误");
        		return mapping.findForward("fail");
        	}

            InputStream stream1 = file.getInputStream(); //把文件读入
            InputStreamReader input1 = new InputStreamReader(stream1);
          
            updatesql1=bean1.getupdatesql(input1, sql2,bean1.getArrayType4(),tablename1,file_index,aa[1]);

        	int count1=0;
        	for(int i=0;i<updatesql1.length;i++){
        		int result_sql=0;
        		try{
        			if(updatesql1[i]!=null&&updatesql1[i].length()>0){
        				result_sql=bean1.update1(updatesql1[i]);
        			}

        		}catch(Exception e){
        			result_sql=0;
            	    bean1.update123("数据导入","SQL语法错误",updatesql1[i].toString().replace("'",""));
            		log.info(bean1.getERRO(e));
        			reuqest1=reuqest1+e.toString()+"<br>";
        		}
        		count1=count1+result_sql;
        	}
        }
        
        try{
        	 bean1.execute1();
        }catch(Exception e){
        	reuqest1=reuqest1+"存储过程有错误:"+e+"<br>";
        	bean1.update123("数据导入", "存储过程有错误", "存储过程有错误");
        	log.info(bean1.getERRO(e));
        	request.setAttribute("result", "存储过程有错误");
    		return mapping.findForward("fail");
        }
        
        try{
        	int customerNum=bean1.getCustomerNum(file_index);
            int cardNum=bean1.getCardNum(file_index);
            reuqest23="客户资料成功导入"+customerNum+"条，失败"+(updatesql.length-customerNum)+"条，卡资料成功"+cardNum+"条，失败"+(updatesql1.length-cardNum)+"条。";
        }catch(Exception e){
        	request.setAttribute("result", "获得结果集错误");
        	log.info(bean1.getERRO(e));
    		return mapping.findForward("fail");
        }
        
        try{
        	int result_a=bean1.update1("insert into system_record values (newid(),'','',getdate(),'数据导入','"+reuqest23+"')");
        }catch(Exception e){
        	request.setAttribute("result", "system_record错误");
        	log.info(bean1.getERRO(e));
    		return mapping.findForward("fail");
        }
        
    	request.setAttribute("result",reuqest23);   	
        return mapping.findForward("success");
        
    	}catch(Exception e){
    		bean1.update123("数据导入","过程错误","过程错误");
    		request.setAttribute("result","过程错误:"+bean1.getERRO(e));  
    		log.info(bean1.getERRO(e));
    		return mapping.findForward("fail");
    	}
    }
    

}
